package com.example.lx5_22.news

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.base.entity.NoticeBeanItem
import com.example.base.net.ApiService

class NoticePagingSource(apiService: ApiService) : PagingSource<Int,NoticeBeanItem>() {
    lateinit var apiService: ApiService
    init {
        this.apiService = apiService
    }

    override fun getRefreshKey(state: PagingState<Int, NoticeBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NoticeBeanItem> {
        try {
            val currentPage = params.key ?: 0
            val notices = apiService.getNotices(currentPage, 10)
            val nextPage = if (notices.data.size == 10) currentPage + 1 else null
            return LoadResult.Page(notices.data,null,nextPage)
        }catch (e:Exception){
            return LoadResult.Error(e)
        }
    }


}